package com.mycompany.j2eem.Aop;

import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *
 * @author wucy
 */
@Aspect
@Component
public class Login {

    @Pointcut("@annotation(com.mycompany.j2eem.Aop.JudgeLog)")
    public void webLog() {
    }

    @Before("webLog()")
    public Object doBefore(JoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        Object args[] = joinPoint.getArgs();
        Object admin = session.getAttribute("username");

        if (admin == null) {
            System.err.println("--------------------------抛出到异常�--------------------------------");
            throw new NoLoginException("No Login!");
        }
        return new JsonResult(ResultCode.SUCCESS);
    }
}
